package com.fafu.control;

import com.fafu.model.Worker;
import com.fafu.pojo.WorkerHistoryPojo;
import com.fafu.service.WorkerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;

import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import util.Dictionary;
import util.JsonRsp;

import java.util.HashMap;
import java.util.Map;

@Api("维修师傅")
@RestController
@RequestMapping("/repairWorker")
public class WorkerControl {
    private Logger logger = LoggerFactory.getLogger(WorkerControl.class);
    @Autowired
    private WorkerService workerService;
    @ApiOperation("维修师傅列表")
    @RequestMapping(value = "/workerList", method = RequestMethod.POST)
    public JsonRsp workerList(
            @RequestParam(value = "key",defaultValue = "",required = false) String key){
        try{
            Map<String,Object> paramMap = new HashMap<>();
            paramMap.put("key",key);
            List<Worker> relist = workerService.searchWorkerList(paramMap);
            for(Worker worker:relist){
                worker.setPassword(null);
                worker.setSalt(null);
            }
            return JsonRsp.success(relist);
        }catch (Exception e) {
            logger.error(e.getMessage());
            return JsonRsp.fail(Dictionary.CODE.SYSTEM_ERROR,"系统内部错误");
        }
    }
    @ApiOperation("维修师傅")
    @RequestMapping(value = "/getWorker", method = RequestMethod.POST)
    public JsonRsp workerList(
            @RequestParam(value = "id")@ApiParam("id") Integer id){
        try{
            Worker worker = workerService.searchWorkerByid(id);
            worker.setPassword(null);
            worker.setSalt(null);
            return JsonRsp.success(worker);
        }catch (Exception e) {
            logger.error(e.getMessage());
            return JsonRsp.fail(Dictionary.CODE.SYSTEM_ERROR,"系统内部错误");
        }
    }
    @ApiOperation("维修师傅的维修List")
    @RequestMapping(value = "/getWorkerWorkList", method = RequestMethod.POST)
    public JsonRsp getWorkerWorkList(
            @RequestParam(value = "id")@ApiParam("id") Integer id){
        try{
//            return JsonRsp.success(worker);
            List<WorkerHistoryPojo> reList = workerService.getHistorylist(id);
            return JsonRsp.success(reList);
        }catch (Exception e) {
            logger.error(e.getMessage());
            return JsonRsp.fail(Dictionary.CODE.SYSTEM_ERROR,"系统内部错误");
        }
    }
}
